/**
 * 
 */
package com.lijin.workshop.dao;

import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.lijin.workshop.util.AssertUtil;

/**
 * @author jin
 *
 */
public abstract class BaseDAO {

	@Autowired
	private SqlSessionFactory sqlSessionFactory;
	
	@SuppressWarnings("unchecked")
	protected <T> T getQryExample(Map<String, String> qryParamMap) {
		Object o = this.buildQryExample(qryParamMap);
		if (o != null) {
			return (T) o;
		} else {
			return null;
		}
	}

	protected SqlSession getSession() {
		return sqlSessionFactory.openSession();
	}
	
	protected <T> T getMapper(Class<T> clazz) {
		SqlSession s = sqlSessionFactory.openSession();
		T mapper = s.getMapper(clazz);
		AssertUtil.validateObjNull(mapper, "");
		return mapper;
	}
	
	protected void closeSession(SqlSession session) {
		if (session != null) {
			session.close();
		}
	}
	
	protected abstract Object buildQryExample(Map<String, String> qryParamMap);
}
